package com.koron.operation.classes.info.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.OrderItem;
import com.koron.bean.query.PageQuery;
import com.koron.operation.classes.info.bean.dto.ClassesDTO;
import com.koron.operation.classes.info.bean.po.Classes;
import com.koron.operation.classes.info.bean.po.ClassesItem;
import com.koron.operation.classes.info.bean.query.ClassesQuery;
import com.koron.operation.classes.info.mapper.ClassesMapper;
import com.koron.operation.classes.info.service.ClassesItemService;
import com.koron.operation.classes.info.service.ClassesService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 班别方案(Classes)表服务实现类
 *
 * @author jzp
 * @since 2021-10-19 19:10:14
 */
@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesItemService classesItemService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public Classes getById(SessionFactory factory, String id) {
        ClassesMapper mapper = factory.getMapper(ClassesMapper.class);
        return mapper.selectById(id);
    }

    @Override
    @TaskAnnotation("getDtoById")
    public ClassesDTO getDtoById(SessionFactory factory, String id) {
        ClassesMapper mapper = factory.getMapper(ClassesMapper.class);
        return mapper.selectDtoById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<Classes> list(SessionFactory factory, ClassesQuery query) {
        ClassesMapper mapper = factory.getMapper(ClassesMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<Classes> page(SessionFactory factory, PageQuery pageQuery, ClassesQuery query) {
        pageQuery.getOrders().add(new OrderItem("sort", true));
        pageQuery.getOrders().forEach(e -> e.setPrefix("classes"));
        return ClassesService.super.page(factory, pageQuery, query);
    }

    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, Classes classes) {
        ClassesMapper mapper = factory.getMapper(ClassesMapper.class);
        if (classes.getSort() == null) {
            Integer maxSort = mapper.getMaxSort();
            classes.setSort(maxSort == null ? 0 : maxSort + 1);
        }
        return mapper.insert(classes) > 0;
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdateDto")
    public boolean saveOrUpdateDto(SessionFactory factory, ClassesDTO classesDTO) {
        // 1. 保存或更新基本数据
        if (StrUtil.isEmpty(classesDTO.getId())) {
            // 1.1. 保存基本数据
            save(factory, classesDTO);
        } else {
            // 1.1. 更新数据，原数据逻辑删除，然后插入一条数据
            deleteById(factory, classesDTO.getId());
            // 1.2. 清空id
            classesDTO.setId(null);
            // 1.3. 保存基本数据
            save(factory, classesDTO);
        }

        // 2. 保存班别列表数据
        List<ClassesItem> items = classesDTO.getItems();
        // 2.1. 设置主表id，并清空id
        items.forEach(e -> {
            e.setClassesId(classesDTO.getId());
            e.setId(null);
        });
        // 2.2. 批量插入
        return classesItemService.saveBatch(factory, items);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        ClassesMapper mapper = factory.getMapper(ClassesMapper.class);
        return mapper.hidden(id) > 0;
    }
}
